package com.mysteel.rabbitmq.topic.consumer;

import com.mysteel.rabbitmq.topic.TopicUtil;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;


/**
 * topic demo 消费端
 *
 * @author Administrator
 */
public class ConsumerApplication {

    public static void main(String[] args) throws Exception {
        // 创建连接和频道
        Channel channel = TopicUtil.createChannel();
        // 声明转发器
        channel.exchangeDeclare(TopicUtil.EXCHANGE_NAME, BuiltinExchangeType.TOPIC);
        // 随机生成一个队列
        String queueName = channel.queueDeclare().getQueue();
        //接收所有与kernel相关的消息
//        for (String bindingKey : TopicUtil.ROUTING_KEYS) {
            channel.queueBind(queueName, TopicUtil.EXCHANGE_NAME, "demo.#");
//        }
        // 处理消息
        DeliverCallback deliverCallback = (consumerTag, delivery) -> {
            String message = new String(delivery.getBody(), "UTF-8");
            System.out.println(" [x] Received '" +
                    delivery.getEnvelope().getRoutingKey() + "':'" + message + "'");
        };
        channel.basicConsume(queueName, true, deliverCallback, consumerTag -> {
        });
    }
}

